package org.geektimes.configuration.micro.config.source.servlet;

import org.eclipse.microprofile.config.Config;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import java.util.logging.Logger;

public class ServletRequestConfigSourceListener implements ServletRequestListener {
    Logger logger = Logger.getLogger(ServletRequestConfigSourceListener.class.getSimpleName());

    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        Config config = (Config) sre.getServletContext().getAttribute(ServletContxtConfitSourceListener.CONTEXT_NAME);
        ThreadLocalConfig.getInstance().getConfigThreadLocal().set(config);
        String applicationName = config.getValue("application.name", String.class);
        logger.info("application.name = [" +applicationName+"]");
    }

    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        ThreadLocalConfig.getInstance().getConfigThreadLocal().remove();
    }
}
